﻿#region Copyright
//————————————————————————————————————————//
//	创建人员(Creator)    ：Huangyd
//	创建日期(Date)       ：2012-08-05
//  联系方式(Email)      ：aj-ayumi@163.com; gajayumi@gmail.com; ajayumi@me.com
//  描    述(Description)：
//
//	Copyright (C) 2012 ajayumi 版权所有
//————————————————————————————————————————//
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ajayumi.Platform.WinForm.WorkSpace
{
    /// <summary>
    /// 工作空间管理类
    /// </summary>
    public class WorkSpacesManager : WorkSpaces
    {


        ///// <summary>
        ///// 显示程序模块
        ///// </summary>
        ///// <param name="parent"></param>
        ///// <param name="name"></param>
        ///// <param name="container"></param>
        //public void ShowModule(IContainerControl parent, string name, Panel container)
        //{
        //    WorkSpaceBase workspace = Instance.GetItem(name);
        //    if (workspace == null) return;

        //    WorkSpaceBase currentWorkSpace = Instance.CurrentWorkSpaceBase;
        //    if (currentWorkSpace != null && currentWorkSpace.IsDisposeControl)
        //    {
        //        //if (Instance.CurrentWorkSpaceBase.Name == name) return;
        //        currentWorkSpace.Dispose();
        //    }

        //    Instance.CurrentWorkSpaceBase = workspace;
        //    workspace.ParentContainer = parent;

        //    //parent.SetTitle(workspace.Title);
        //    //parent.SetDescription(workspace.Description);

        //    ContainerControl containerControl = workspace.ContainerCtl;
        //    if (!container.Controls.Contains(containerControl))
        //    {
        //        containerControl.Dock = DockStyle.Fill;
        //        container.Controls.Clear();
        //        container.Controls.Add(containerControl);
        //        //parent.LoadToolStripItems(workspace.InitToolStripItems);
        //    }

        //    RaiseWorkSpaceChanged();
        //}

        ///// <summary>
        ///// 显示程序模块
        ///// </summary>
        ///// <param name="parent"></param>
        ///// <param name="name"></param>
        ///// <param name="container"></param>
        //public static void ShowModule(IContainerControl parent, string name, Form parentForm)
        //{
        //    WorkSpaceBase workspace = Instance.GetItem(name);
        //    if (workspace == null) return;

        //    WorkSpaceBase currentWorkSpace = Instance.CurrentWorkSpaceBase;
        //    if (currentWorkSpace != null && currentWorkSpace.IsDisposeControl)
        //    {
        //        //if (Instance.CurrentWorkSpaceBase.Name == name) return;
        //        currentWorkSpace.Dispose();
        //    }

        //    Instance.CurrentWorkSpaceBase = workspace;
        //    workspace.ParentFormMain = parent;

        //    parent.SetTitle(workspace.Title);
        //    parent.SetDescription(workspace.Description);

        //    ContainerControl containerControl = workspace.ContainerCtl;
        //    Form frm = (containerControl as Form);
        //    frm.Activated += (sender, e) =>
        //    {
        //        parent.LoadToolStripItems(workspace.InitToolStripItems);
        //    };
        //    frm.MdiParent = parentForm;
        //    frm.Show();

        //    RaiseWorkSpaceChanged();
        //}

        //internal static void ShowModule(IMainForm parent, string name, Panel container)
        //{
        //    WorkSpaceBase workspace = WorkSpacesManager.GetItem(name);

        //    if (Instance.CurrentWorkSpaceBase != null)
        //    {
        //        if (Instance.CurrentWorkSpaceBase.Name == name) return;
        //        Instance.CurrentWorkSpaceBase.Dispose();
        //    }

        //    Instance.CurrentWorkSpaceBase = workspace;
        //    workspace.ParentFormMain = parent;

        //    parent.SetTitle(workspace.Title);
        //    parent.SetDescription(workspace.Description);

        //    UserCtlPanelBase pnl = workspace.UserCtlPnl;
        //    if (!container.Controls.Contains(pnl))
        //    {
        //        pnl.Dock = DockStyle.Fill;
        //        container.Controls.Clear();
        //        container.Controls.Add(pnl);
        //        parent.LoadToolStripItems(workspace.InitToolStripItems);
        //    }

        //    RaiseWorkSpaceChanged();
        //}

        private static WorkSpaces m_Instance;
        public static WorkSpaces Instance
        {
            get
            {
                if (m_Instance == null)
                {
                    m_Instance = new WorkSpacesManager();
                }
                return m_Instance;

            }
        }

        protected WorkSpacesManager()
        {
#if DEBUG
            Console.WriteLine("{0} {1}实例化", DateTime.Now, this.ToString());
#endif
        }

        public override void ShowModule(string name, IParentContainer parent)
        {
            WorkSpaceBase workspace = this.GetItem(name);
            if (workspace == null) return;

            WorkSpaceBase currentWorkSpace = this.CurrentWorkSpaceBase;
            if (currentWorkSpace != null && currentWorkSpace.Name.Equals(name))
            {
                //if (Instance.CurrentWorkSpaceBase.Name == name) return;
                if (currentWorkSpace.IsDisposeControl)
                {
                    this.Collection.Remove(currentWorkSpace);
                    currentWorkSpace.Dispose();
                }
                return;
            }

            this.CurrentWorkSpaceBase = workspace;
            workspace.ParentContainer = parent;


            //ContainerControl containerControl = workspace.ContainerCtl;
            //Form frm = (containerControl as Form);
            //frm.Activated += (sender, e) =>
            //{
            //    parent.SetTitle(workspace.Title);
            //    parent.SetDescription(workspace.Description);
            //    parent.LoadToolStripItems(workspace.InitToolStripItems);
            //};
            //frm.Deactivate += (sender, e) =>
            //{
            //    workspace.UnRegisterAction();
            //};
            //frm.MdiParent = (Form)parent;
            //frm.Show();
            parent.RenderInContainer(workspace);

            RaiseWorkSpaceChanged();
        }

    }
}
